<template>
  <div class="wrapper">
    <upload-excel :on-success="onSuccess" />
  </div>
</template>

<script>
import { getGoodsTypeAPI, importGoods } from '@/api/goods'
export default {
  data() {
    return {
      goodsType: []
    }
  },
  created() {
    this.getGoodsType()
  },
  methods: {
    async onSuccess({ header, results }) {
      const data = results.map(item => {
        const goods = {}
        goods.skuName = item['商品名称']
        goods.brandName = item['品牌']
        goods.price = item['商品价格']
        // this.goodsType.forEach(el => {
        //   console.log(item['商品类型'], el.className)
        //   if (item['商品类型'] === el.className) {
        //     goods.classId = el.classId
        //   }
        // })

        // goods.className = item['商品类型']
        goods.unit = item['规格']
        return goods
      })
      const formData = new FormData()
      formData.append('data', data)
      await importGoods(formData)
      this.$message.success('恭喜，添加成功！！')
      this.$router.back()
    },
    // 商品类型
    async getGoodsType() {
      const res = await getGoodsTypeAPI()
      const goodsType = res.data.currentPageRecords
      this.goodsType = goodsType
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper{
  height: 80vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
